package com.captjack.micro.api.hello;

import com.captjack.micro.api.constant.Constant;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Capt Jack
 * @date 2018/4/17
 */
@FeignClient(name = Constant.HELLO_MICRO_SERVICE_NAME, fallback = MicroServiceOne.FallbackMicroServiceOne.class)
@RequestMapping(value = "/microServiceOne", method = {RequestMethod.GET, RequestMethod.POST})
public interface MicroServiceOne {

    /**
     *
     */
    String SERVICE_URL = "http://" + Constant.HELLO_MICRO_SERVICE_NAME + "/microServiceOne/message";

    /**
     * 测试
     *
     * @param num1 参数一
     * @param num2 参数二
     * @return String   返回
     * @throws Exception 异常
     */
    @RequestMapping(value = "/message")
    String service(@RequestParam(value = "num1") int num1, @RequestParam(value = "num2") int num2) throws Exception;

    /**
     * fallback实现
     */
    @Component
    final class FallbackMicroServiceOne implements FallbackFactory<MicroServiceOne> {

        @Override
        public MicroServiceOne create(Throwable throwable) {
            return (num1, num2) -> {
                throwable.printStackTrace();
                return Constant.DEFAULT_HYSTRIX_MESSAGE;
            };
        }

    }

}
